package ext.generic.part;

import java.io.*;

import wt.doc.*;

import java.util.*;

import java.text.*;

import wt.fc.*;

import wt.lifecycle.*;

import wt.epm.*;

import wt.pom.*;

import wt.part.*;

import wt.services.*;

import wt.inf.container.*;

import wt.util.*;

import wt.vc.wip.*;

import wt.session.*;

import wt.org.*;

import wt.events.KeyedEvent;
import wt.events.KeyedEventDispatcher;

import wt.vc.*;

import wt.query.*;

import wt.enterprise.RevisionControlled;

import wt.iba.value.*;

/**
 *有关零部件的监听程序
 */
public class StandardGenericPartService extends StandardManager implements GenericPartService, Serializable
{
    public static boolean VERBOSE;
    public static WTProperties wtproperties;
    private static final String CLASSNAME = StandardGenericPartService.class.getName();
    private ServiceEventListenerAdapter listener;
    private static String logs_dir = "";

    static
    {
        wtproperties = null;
        try
        {
            wtproperties = WTProperties.getLocalProperties();
            VERBOSE = wtproperties.getProperty("ext.generic.part.verbose", false);
            logs_dir = wtproperties.getProperty("wt.logs.dir", "");
        }
        catch (Throwable t)
        {
            throw new ExceptionInInitializerError(t);
        }
    }

    public StandardGenericPartService()
    {
    }

    public String getConceptualClassname()
    {
        return CLASSNAME;
    }
    /**
     *初始化
     * @return
     * @throws WTException
     */
    public static StandardGenericPartService newStandardGenericPartService() throws WTException
    {
        StandardGenericPartService standardmpereviseservice = new StandardGenericPartService();
        standardmpereviseservice.initialize();
        return standardmpereviseservice;
    }
    /**
     *服务启动方法,其中注册需要监听的事件(消息)
     * @throws ManagerException
     */
    protected void performStartupProcess() throws ManagerException
    {
        System.out.println("Starting Generic Part service..");
        listener = new GenericPartServiceEventListener(getConceptualClassname());
        //getManagerService().addEventListener(listener, PersistenceManagerEvent.generateEventKey("POST_STORE"));
        //getManagerService().addEventListener(listener, PersistenceManagerEvent.generateEventKey("PRE_STORE"));
        getManagerService().addEventListener(listener, VersionControlServiceEvent.generateEventKey("NEW_VERSION"));
        //getManagerService().addEventListener(listener,LifeCycleServiceEvent.generateEventKey("STATE_CHANGE"));
        //getManagerService().addEventListener(listener,LifeCycleServiceEvent.generateEventKey("SET_STATE"));
        //getManagerService().addEventListener(listener, IdentityServiceEvent.generateEventKey("POST_CHANGE_IDENTITY"));		
    }
    /**
     * 事件监听的子类
     */
    class GenericPartServiceEventListener extends ServiceEventListenerAdapter
    {
        /**
         *具体的监听方法
         * @param obj 消息(事件)
         * @throws WTException
         * @throws WTPropertyVetoException
         * @throws IOException
         * @throws Exception
         */
        public void notifyVetoableEvent(Object obj) throws WTException, WTPropertyVetoException, IOException, Exception
        {

            KeyedEvent keyedevent = (KeyedEvent) obj;
            Object wtObject = keyedevent.getEventTarget();
            String eventType = keyedevent.getEventType();

            //添加修订事件
            if (obj instanceof VersionControlServiceEvent)
            {
                VersionControlServiceEvent versionControlServiceEvent = (VersionControlServiceEvent) obj;
                Object obj1 = versionControlServiceEvent.getEventTarget();
                if (!versionControlServiceEvent.getEventType().equals("NEW_VERSION"))
                    return;
                if (!(obj1 instanceof Versioned))
                    return;
                //清除从旧版本或复制对象带过来的IBA属性
                String version = "";
                String iteration = "";
                version = VersionControlHelper.getVersionIdentifier((Versioned) obj1).getValue();
                iteration = VersionControlHelper.getIterationIdentifier((Iterated) obj1).getValue();
                String objVersion = version + "." + iteration;
                if (VERBOSE)
                {
                    System.out.println("	current object iteration identity=" + objVersion);
                }
                //只处理修订情况
                if ((!version.equals("A")) && iteration.equals("1"))
                {
                    obj1 = PartWorkflowUtil.clearHistoricalIBA((WTObject) obj1, "revise");
                }
                else
                {
                    return;
                }
            }
            else
            {
                return;
            }
        }

        public GenericPartServiceEventListener(String s)
        {
            super(s);
        }
    }
}
